package com.lzx.resumeweb.api;

import com.lzx.resumebase.model.Result;
import com.lzx.resumebase.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName EmailApi
 * @Author 刘正星
 * @Date 2021/6/10 下午3:13
 * @Description
 */
@RestController
@RequestMapping("/email")
public class EmailApi {

    @Autowired
    private JavaMailSender javaMailSender;
    @Value("${address}")
    private String sendEmailAddress;

    @RequestMapping(path = "/send")
    @ResponseBody
    public Result<SimpleMailMessage> sendMessage(@RequestParam("name") String name,@RequestParam("msg")String msg){

        SimpleMailMessage message = new SimpleMailMessage();
        if (!StringUtils.hasText(name)||!StringUtils.hasText(msg)){
            return ResultUtil.paramError();
        }
        message.setFrom(sendEmailAddress);
        message.setTo(sendEmailAddress);
        message.setSubject("来自"+name+"的消息");
        message.setText(msg);
        javaMailSender.send(message);
        return ResultUtil.success(message);
    }

}
